Blog

Why is Code Reuse Important?

Gavin Pickin May 02, 2022

Spread the word

Gavin Pickin

May 02, 2022

Spread the word


Share your thoughts

I know we all reuse code and some types of reuse are better than the others (I'm looking at you COPY AND PASTE / man in the mirror), but copy-paste is only a symptom of a larger issue, that WET code is not as easy to maintain as DRY code.

What is DRY Code?

Don't Repeat Yourself (DRY) code aims to reduce code repetition. We do not want to reinvent the wheel, but worse is having bad versions of the wheel scattered everywhere. The goal of DRY code is to refactor so that you only have one reusable instance of that code, and if you need to change it, you can do so in one place. It is easier to maintain and update. DRY code also reduces the chance of error since you are only updating one place instead of several. We have all used DRY code when we have used loops, functions, or includes.

What is WET code?

Write Everything Twice (WET) code is the opposite of DRY code. It is where you have duplicate code copy/pasted, or rewritten several times in multiple places. Sometimes WET code is DRY code that needs minor changes, but you make a copy of it. If it is a simple change, it might be ok to add it to your DRY code, but sometimes it's better not to over-engineer things. DRY code is the goal in most cases, and the benefits will outweigh the costs.

Why do we want DRY Code?

The biggest benefit of using DRY code is maintainability. Fixing bugs in one location is easier than remembering and finding all the occurrences of that logic. Likewise, updating the business logic or adding functionality is easier when the code is shared.

An Acronym used with DRY code is FIRST, which stands for Focused, Independent, Reusable, Small, and Testable (https://addyosmani.com/first/)

Readability

Programs must be written for people to read, and only incidentally for machines to execute - Harold Abelson

If a developer understands the principles of DRY code, they are more likely to understand clean code principles, leading to more readable code. Code reuse usually encourages smaller, more manageable functions, promoting lower cognitive load and making the code more readable.

Testing

DRY code is broken down into smaller pieces and is usually easier to test. The more code you have and the more paths and functions to cover, the harder it will be to test. Using DRY Code from libraries and frameworks means more eyes on it, and sometimes those libraries and frameworks have tests for their code already.

Cost

  • More code takes more time and money
  • Reinventing the wheel takes more time and money
  • Maintaining more takes costs more time and money
  • Testing more code takes more time and money
  • Fixing more bugs takes more time and money

How do we prevent Copy Pasting and WET Code?

We need fast and simple ways to share code in a project and between projects. We are lucky in ColdFusion in that we have lots of techniques and tools to help us write DRYer code. Check back for our next blog entry, where we go into more detail.

Add Your Comment

(1)

May 09, 2022 15:51:00 UTC

by Jason

Thanks for this Gavin. Tryng to convince other members in our crew to adopt this philosophy is somewhat challenging.

Recent Entries

BoxLang 1.0.0 Beta 22 Launched

BoxLang 1.0.0 Beta 22 Launched

The BoxLang 1.0.0-Beta22 release includes several improvements, bug fixes, new features, and stories. Key improvements include enhanced redirection for the Miniserver, better transactional event broadcasting, and added convenience methods like getRequestContext() and getApplicationContext(). Bug fixes address issues such as JSON deserialization, whitespace management, and various errors related to data types and loops. New features include support for multiple statements inside queries and a new datasourceRegister() BIF for easier SaaS integrations.

Jon Clausen
Jon Clausen
November 15, 2024
Is Cloud the Answer for Your ColdFusion Dilemma?

Is Cloud the Answer for Your ColdFusion Dilemma?

Feeling the limits of an on-premise ColdFusion setup?

Many businesses face high costs, limited scalability, and performance bottlenecks, leaving them wondering if the cloud could be the answer.

In our FREE whitepaper, "Is Cloud the Answer for Your ColdFusion Dilemma?", we explore:

  • Benefits of Migrating to the Cloud: From cost savings to increased flexibility, find out what a cloud-based ColdFusion setup can do.
Cristobal Escobar
Cristobal Escobar
November 13, 2024
Mastering Events and Listeners in CBWIRE

Mastering Events and Listeners in CBWIRE

In CBWIRE, events and listeners are the backbone of building responsive, modular applications without relying heavily on JavaScript. This guide walks you through setting up and using CBWIRE events to create seamless interactions between components, from dispatching events in CFML and frontend templates to listening with Alpine.js and JavaScript. Learn how to make your applications feel dynamic and engaging by effortlessly connecting components. Whether you’re triggering events to update a dashboard or targeting specific parts of your app with dispatchTo, these techniques will empower you to create a modern, interactive CFML experience with ease.

Grant Copley
Grant Copley
November 11, 2024